package com.qfedu.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qfedu.dao.GradeDao;
import com.qfedu.entity.Grade;
import com.qfedu.entity.GradeWithStu;
import com.qfedu.entity.ResponseData;
import com.qfedu.service.GradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
public class GradeServiceImpl implements GradeService {
    @Autowired
    private GradeDao gradeDao;

    @Override
    public ResponseData<List<Grade>> getGradeList(int pageIndex,int pageSize) {

        Page page = PageHelper.startPage(pageIndex, pageSize);
        List<Grade> allGrade = gradeDao.getAllGrade();
        PageInfo pageInfo = new PageInfo(allGrade);
        int count =(int) pageInfo.getTotal();//总条数


        ResponseData<List<Grade> > responseData = new ResponseData<>();

        responseData.setCode(0);
        responseData.setMsg(ResponseData.SUCCESS_MSG);
        responseData.setData(allGrade);
        responseData.setCount(count);

        return responseData;
    }

    @Override
    public int updateGrade(Grade grade) {

        return gradeDao.updateGrade(grade);
    }

    @Override
    public int deleteGradeById(int gid) {
        return gradeDao.deleteGradeById(gid);
    }

    @Override
    public int insertGrade(Grade grade) {
        return gradeDao.insertGrade(grade);
    }

    @Override
    public GradeWithStu getGradeNameWithStuNum() {
        List<Map> stuNumWithGrade = gradeDao.getStuNumWithGrade();

        List<String> gradeNameList = new ArrayList<>();
        List<Integer> studentNumList = new ArrayList<>();

        for (Map map :
                stuNumWithGrade) {
            gradeNameList.add((String)map.get("name"));
            Long num1=(Long)map.get("num");
            studentNumList.add(num1.intValue());
        }
        GradeWithStu gradeWithStu = new GradeWithStu();
        gradeWithStu.setGradeNameList(gradeNameList);
        gradeWithStu.setStudentNumList(studentNumList);

        return gradeWithStu;
    }

    @Override
    public List selectGrade() {
        return gradeDao.getGradeNameId();
    }
}
